<?php
class AccountController extends Zend_Controller_Action {
	
	// Mang tham so nhan duoc o moi Action
	protected $_arrParam;
	private $USER_ID = 0;
	private $URI = "/";
	private $NON_DISPLAY = 0;
	private $ALERT_TYPE_ERROR = "alert-error";
	private $ALERT_TYPE_SUCCESS = "alert-success";
	
	// Define URL
	private $URL_PROF_VIEW = "/profile/view/";
	private $URL_ACC_LOGIN = "/account/login/";
	private $URL_ACC_REG = "/account/register/";
	private $URL_ADM = "/admin/";
	public function init() {
		// Mang tham so nhan duoc o moi Action
		$this->_arrParam = $this->_request->getParams ();
		// Xu li cho phan header
		$this->URI = $this->getRequest ()->getScheme () . "://" . $this->getRequest ()->getHttpHost () . $this->getRequest ()->getRequestUri ();
		$this->view->return_url = $this->URI;
	}
	public function loginAction() {
		$mAccount = new Default_Model_Account ();
		$mUser = new Default_Model_User ();
		if (! $mAccount->hasIdentity ()) {
			if ($this->_request->isPost ()) {
				if (! isset ( $_POST ['email'] )) {
					// redirect processing
					if ($mAccount->login ( $this->_arrParam )) {
						if ($mAccount->getRole () == 1) {
							// update user info
							$arrParam = array (
									'last_login' => date ( 'Y-m-d H:i:s' ) 
							);
							if ($mUser->updateUserInfo ( $arrParam, $mAccount->getUserId() )) {
								if (isset ( $this->_arrParam ['return_url'] )) {
									$this->_redirect ( $this->_arrParam ['return_url'] );
								} else {
									$this->_redirect ( "/" );
								}
							} else {
								$this->view->display_alert = 1;
								$this->view->alert_type = $this->ALERT_TYPE_ERROR;
								$this->view->alert_message = '<strong>Error!</strong> ' . $mUser->getError ();
							}
						} else {
							// logout
							$mAccount->logout ();
							$this->view->display_alert = 1;
							$this->view->alert_type = "";
							$this->view->alert_message = "<strong>Warning!</strong> Bạn không có quyền truy cập trang quản trị.";
						}
					} else {
						$this->view->display_alert = 1;
						$this->view->alert_type = $this->ALERT_TYPE_ERROR;
						$this->view->alert_message = '<strong>Error!</strong> Tên đăng nhập hoặc mật khẩu không đúng.';
					}
				} else {
					$arrCkEmail = $mUser->checkExistingEmail ( $_POST ['email'] );
					if (count ( $arrCkEmail ) == 0) {
						$this->view->display_alert = 1;
						$this->view->alert_type = $this->ALERT_TYPE_ERROR;
						$this->view->alert_message = '<strong>Error!</strong> Email không tồn tại.';
					} else {
						// generate pwd
						$strNewPwd = YourTour_Utility_String::generatePassword ();
						$arrParam = array (
								'password' => md5 ( $strNewPwd ),
								'updated_time' => date ( 'Y-m-d H:i:s' ) 
						);
						if ($mUser->updateUserInfo ( $arrParam, $arrCkEmail [0] ['user_id'] )) {
							// send email welcome
							try {
								$strSubjectMail = "YourTour - Khôi phục mật khẩu";
								$strBodyMail = "Bạn đã yêu cầu khôi phục lại mật khẩu truy cập YourTour\nMật khẩu mới của bạn là :" . $strNewPwd . "\nXin chân thành cảm ơn!";
								$arrRecipients [0] ['email'] = $_POST ['email'];
								$arrRecipients [0] ['name'] = $arrCkEmail [0] ['username'];
								$strSendMailReturn = YourTour_PHPMailer_PHPMailerFactory::sendMail ( $strSubjectMail, $strBodyMail, $arrRecipients );
								if ($strSendMailReturn == "") {
									$this->view->display_alert = 1;
									$this->view->alert_type = $this->ALERT_TYPE_SUCCESS;
									$this->view->alert_message = 'Chúng tôi đã gửi mật khẩu mới đến email của bạn, hãy kiểm tra và đăng nhập lại!';
								} else {
									$this->view->display_alert = 1;
									$this->view->alert_type = $this->ALERT_TYPE_ERROR;
									$this->view->alert_message = '<strong>Error!</strong> ' . $strSendMailReturn;
								}
							} catch ( Exception $e ) {
								$this->view->display_alert = 1;
								$this->view->alert_type = $this->ALERT_TYPE_ERROR;
								$this->view->alert_message = '<strong>Error!</strong> ' . $e->getError ();
							}
						} else {
							$this->view->display_alert = 1;
							$this->view->alert_type = $this->ALERT_TYPE_ERROR;
							$this->view->alert_message = '<strong>Error!</strong> ' . $mUser->getError ();
						}
					}
				}
			}
		} else {
			$this->_redirect ( '/' );
		}
	}
	
	public function logoutAction() {
		$mAccount = new Default_Model_Account ();
		if (! $mAccount->hasIdentity ()) {
			$this->_redirect ( $this->URL_ACC_LOGIN );
		} else {
			$mAccount->logout ();
			$this->_redirect ( $this->URL_ACC_LOGIN );
		}
	}
}